//
//  FYVideoPlayVC.m
//  diantai
//
//  Created by 吕斌利 on 2019/8/26.
//  Copyright © 2019 吕斌利. All rights reserved.
//

#import "FYVideoPlayVC.h"
#import "model.h"
#import "FYVideoPlayVC.h"
#import "WMPlayer.h"
#import "mainViewController.h"
#import "CDYRippleView.h"
#import "mianzeViewController.h"
@interface FYVideoPlayVC ()<WMPlayerDelegate>
@property (nonatomic, strong) UILabel*titleLabel;
@property (nonatomic, copy) NSString*type;
@property (nonatomic, strong) WMPlayer*wmPlayer;
@end

@implementation FYVideoPlayVC

- (void)viewDidLoad {
    _type=@"1";
    if (self.data.count>0) {
        [self initUI];
    }
    else{
        [self request];
    }
    
}
-(void)initUI{
    UIButton *left = [UIButton buttonWithType:UIButtonTypeCustom];
    [left setImage:[UIImage imageNamed:@"caidan"] forState:0];
    WeakSelf
    [self.navigationView addSubview:left clickCallback:^(UIView *view) {
        StrongSelf
        mainViewController *mo= [mainViewController new];
        WeakSelf
        mo.delegetBlock = ^(NSArray * _Nonnull data, NSInteger inx) {
            StrongSelf
            self.data =data;
            self.inx =inx;
            
            model *mode=self.data[self.inx];
            [self.wmPlayer resetWMPlayer];
            WMPlayerModel*model=[[WMPlayerModel alloc]init];
            model.videoURL=[NSURL URLWithString:mode.radioUrl];
            self.wmPlayer.playerModel=model;
            self.titleLabel.text = mode.name;
            [self.wmPlayer play];
            
            
        };
        [self.navigationController pushViewController:mo animated:YES];
    }];
    [left mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(20);
        make.height.offset(20);
        make.left.offset(15);
        make.top.offset(kStatusBarHeight+10);
    }];
    
    UIButton *right = [UIButton buttonWithType:UIButtonTypeCustom];
    [right setImage:[UIImage imageNamed:@"wode-guanyuwomen"] forState:0];
   
    [self.navigationView addSubview:right clickCallback:^(UIView *view) {
        StrongSelf
        mianzeViewController *mo= [mianzeViewController new];
        [self.navigationController pushViewController:mo animated:YES];
    }];
    [right mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(20);
        make.height.offset(20);
        make.right.offset(-15);
        make.top.offset(kStatusBarHeight+10);
    }];
    self.titleLabel = [UILabel new];
    self.titleLabel.textAlignment=NSTextAlignmentCenter;
    self.titleLabel.text = @"wexshbhbhbjhhbhbhjhjbbjhhjbjhjbhjhjhj";
    self.titleLabel.font =self.navigationView.titleLabel.font;
    self.titleLabel.textColor =self.navigationView.titleLabel.textColor;
    [self.navigationView addSubview:self.titleLabel clickCallback:nil];;
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(150);
        make.height.offset(44);
        make.centerX.offset(0);
        make.top.offset(kStatusBarHeight);
        
    }];
    self.titleLabel.text =self.titlesre;
    WMPlayerModel*model=[[WMPlayerModel alloc]init];
    model.videoURL=[NSURL URLWithString:self.videoString];
    WMPlayer*wmPlayer=[[WMPlayer alloc]initPlayerModel:model];
    wmPlayer.enableBackgroundMode=NO;
    wmPlayer.enableFastForwardGesture=NO;
    
    wmPlayer.frame=CGRectMake(0, 0, 1, 1);
    wmPlayer.backBtnStyle=BackBtnStylePop;
    wmPlayer.delegate=self;
    self.wmPlayer=wmPlayer;
    [self.view addSubview:wmPlayer];
    [wmPlayer play];
    
    UIButton * bu = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:bu];
    [bu setImage:[UIImage imageNamed:@"zantingtingzhi"] forState:UIControlStateSelected];
    [bu setImage:[UIImage imageNamed:@"weibiaoti"] forState:0];
    [bu mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.offset(-150);
        make.width.height.offset(40);
    }];
    [bu addTarget:self action:@selector(bofang:) forControlEvents:UIControlEventTouchUpInside];
    
    bu.selected = YES;
    
    
    UIButton * shangyi = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:shangyi];
    [shangyi setImage:[UIImage imageNamed:@"shangyiqu101"] forState:0];
    
    [shangyi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(50);
        make.bottom.offset(-150);
        make.width.height.offset(40);
    }];
    [shangyi addTarget:self action:@selector(shangyiqu) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton * xiayique = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:xiayique];
    [xiayique setImage:[UIImage imageNamed:@"xiayiqu"] forState:0];
    
    [xiayique mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-50);
        make.bottom.offset(-150);
        make.width.height.offset(40);
    }];
    [xiayique addTarget:self action:@selector(xiayique) forControlEvents:UIControlEventTouchUpInside];
    UIImageView *iamge=[UIImageView new];
    iamge.image=UIImageWithName(@"Unknown");
    iamge.frame=CGRectMake(0, 0, AdaptedWidth(200), AdaptedWidth(200));
    iamge.center=self.view.center;
    iamge.layer.cornerRadius =AdaptedWidth(200)*0.5;
    
    //将多余的部分切掉
    iamge.layer.masksToBounds = YES;
    [self.view addSubview:iamge];
    CDYRippleView * rippleView = [[CDYRippleView alloc] initWithFrame:CGRectMake(0, 0, AdaptedWidth(200)/sqrt(2), AdaptedWidth(200)/sqrt(2))];
    rippleView.center = self.view.center;
    rippleView.backgroundColor = kClearColor;
    [self.view addSubview:rippleView];
    
}
- (void)request {
    NSString *urlStr = @"http://fm.gerenhao.com/api/open/voice/union/select/1.3.9.json";
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    //创建request
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.HTTPMethod = @"POST";
    request.HTTPBody = [@"level=3&pageIndex=1&parentId=31&rootId=1" dataUsingEncoding:NSUTF8StringEncoding];
    request.HTTPShouldUsePipelining = YES;
    //创建NSURLSession
    NSURLSession *session = [NSURLSession sharedSession];
    //sessio÷÷
    //创建任务
    WeakSelf
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSDictionary *jsonInfo = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        StrongSelf
        
        self.data =  [NSArray modelArrayWithClass:[model class] json:[jsonInfo objectForKey:@"result"]];
        WeakSelf
        dispatch_async(dispatch_get_main_queue(), ^{
            StrongSelf
            self.inx =0;
             model *mode=self.data[self.inx];
            self.videoString = mode.radioUrl;
            self.titlesre = mode.name;
           [self initUI];
        });
        
    }];
    
    //开始任务
    [task resume];
}
-(void)shangyiqu{
    if (self.inx == 0) {
        self.inx = self.data.count-1;
    }
    else {
        self.inx =self.inx-1;
    }
    
    model *mode=self.data[self.inx];
    [self.wmPlayer resetWMPlayer];
    WMPlayerModel*model=[[WMPlayerModel alloc]init];
    model.videoURL=[NSURL URLWithString:mode.radioUrl];
    self.wmPlayer.playerModel=model;
    self.titleLabel.text = mode.name;
    [self.wmPlayer play];
   
}

-(void)xiayique{
    if (self.inx == self.data.count-1) {
        self.inx = 0;
    }
    else {
        self.inx =self.inx+1;
    }
    
    model *mode=self.data[self.inx];
    [self.wmPlayer resetWMPlayer];
    WMPlayerModel*model=[[WMPlayerModel alloc]init];
    model.videoURL=[NSURL URLWithString:mode.radioUrl];
    self.wmPlayer.playerModel=model;
    self.titleLabel.text = mode.name;
    [self.wmPlayer play];
   
    
}
- (void)bofang:(UIButton*)seder{
    if (seder.selected) {
        [self.wmPlayer pause];
    }
    else{
        [self.wmPlayer play];
    }
    
    seder.selected = !seder.selected;
}
-(void)wmplayer:(WMPlayer *)wmplayer clickedCloseButton:(UIButton *)backBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)wmplayer:(WMPlayer *)wmplayer isHiddenTopAndBottomView:(BOOL )isHidden{
    
}
-(void)wmplayerFailedPlay:(WMPlayer *)wmplayer WMPlayerStatus:(WMPlayerState)state{
    if ([self.type isEqualToString:@"1"]) {
    
        model *mode=self.data[self.inx];
        [self.wmPlayer resetWMPlayer];
        WMPlayerModel*model=[[WMPlayerModel alloc]init];
        model.videoURL=[NSURL URLWithString:mode.backUrl];
        self.wmPlayer.playerModel=model;
        self.titleLabel.text = mode.name;
        [self.wmPlayer play];
        self.type = @"2";
    }
    else{
        if (self.inx == self.data.count-1) {
            self.inx = 0;
        }
        else {
            self.inx =self.inx+1;
        }
        
        model *mode=self.data[self.inx];
        [self.wmPlayer resetWMPlayer];
        WMPlayerModel*model=[[WMPlayerModel alloc]init];
        model.videoURL=[NSURL URLWithString:mode.radioUrl];
        self.wmPlayer.playerModel=model;
        self.titleLabel.text = mode.name;
        [self.wmPlayer play];
        self.type =@"1";
    }
    
    
}
-(void)wmplayerReadyToPlay:(WMPlayer *)wmplayer WMPlayerStatus:(WMPlayerState)state{
    
}
-(void)wmplayerFinishedPlay:(WMPlayer *)wmplayer{
    if ([self.type isEqualToString:@"1"]) {
        
        model *mode=self.data[self.inx];
        [self.wmPlayer resetWMPlayer];
        WMPlayerModel*model=[[WMPlayerModel alloc]init];
        model.videoURL=[NSURL URLWithString:mode.backUrl];
        self.wmPlayer.playerModel=model;
        self.titleLabel.text = mode.name;
        [self.wmPlayer play];
        self.type = @"2";
    }
    else{
        if (self.inx == self.data.count-1) {
            self.inx = 0;
        }
        else {
            self.inx =self.inx+1;
        }
        
        model *mode=self.data[self.inx];
        [self.wmPlayer resetWMPlayer];
        WMPlayerModel*model=[[WMPlayerModel alloc]init];
        model.videoURL=[NSURL URLWithString:mode.radioUrl];
        self.wmPlayer.playerModel=model;
        self.titleLabel.text = mode.name;
        [self.wmPlayer play];
        self.type =@"1";
    }
}

@end
